package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinFindProductService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/18
 * Description:集楼_帮我找房_小程序_接口
 */

@Controller
@RequestMapping("/sunday/small/findProduct")
@Api(value = "findProduct",description = "帮我找房")
public class SundaySmallFindProductController extends BaseController {
    @Autowired
    private SundayWeiXinFindProductService findProductService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation("【小程序】帮我找房")
    public void select(HttpServletRequest request, HttpServletResponse response,
                       @ApiParam(value = "用户id",required = true)@RequestParam("memberId")Long memberId,
                       @ApiParam(value = "最小价格",required = true)@RequestParam("minPrice")int minPrice,
                       @ApiParam(value = "最大价格",required = true)@RequestParam("maxPrice")int maxPrice,
                       @ApiParam(value = "房型: 三室/四室..",required = true)@RequestParam("roomNum")String roomNum,
                       @ApiParam(value = "类型: 住宅/别野...",required = true)@RequestParam("productType")String productType,
                       @ApiParam(value = "关注区域",required = true)@RequestParam(value = "area")String area,
                       @ApiParam(value = "联系方式: 手机或者微信号",required = true)@RequestParam(value = "contactWay")String contactWay
    ){
        try {
            findProductService.save(memberId,minPrice,maxPrice,roomNum,productType,area,contactWay);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/findProduct/save----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }
}
